<?php
declare (strict_types=1);

namespace app\index\controller;

use app\common\component\Upload;
use app\common\model\Banner;
use app\common\model\Disease;
use app\common\model\Project;
use app\common\model\Type;
use think\App;

class Index extends Base
{
    public function __construct(App $app)
    {
        parent::__construct($app);

    }

    public function index()
    {
        return $this->fetch();
    }

    public function banner(Banner $banner)
    {
        $list = $banner->field('id,image')->order('sort', 'asc')->order('id', 'desc')->select();
        ajaxSuccess($list);
    }

    public function upload()
    {
        $result = Upload::instance()->uploadFile();
        ajaxSuccess($this->request->domain() . $result['data']['url']);
    }

    public function project()
    {
        $project_id = $this->request->get('id', 1);
        $project = Project::where('id', $project_id)->find();
        ajaxSuccess($project);
    }

    public function projectSave(\app\admin\validate\Project $projectValide, Project $project)
    {
        $postData = $this->request->post(['id' => 1, 'title' => '', 'start_date' => '', 'end_date' => '', 'length' => '', 'structure' => '']);
        $valid = $projectValide->check($postData);
        if (!$valid) ajaxError($projectValide->getError());
        $exits = $project->where('id', $postData['id'])->count();
        if ($exits > 0) {
            $result = $project->update($postData);
        } else {
            $result = $project->save($postData);
        }
        if (!$result) ajaxError('保存失败');
        ajaxSuccess();
    }

    public function cad(Project $project)
    {
        $postData = $this->request->post(['id' => 1, 'cad' => '']);
        if (!$postData['cad']) ajaxError('请上传图片');
        if (!in_array($postData['id'], [1, 2])) ajaxError('请选择正确的项目');
        $project->where('id', $postData['id'])->update([
            'cad' => $postData['cad'],
        ]);
        ajaxSuccess();
    }

    public function getFile(Disease $disease)
    {

        $project_id = 2;
        try {
            $fileName = $disease->createXlsx($project_id);
            ajaxSuccess($this->request->domain() . $fileName);
        } catch (\Exception $exception) {
            ajaxError($exception->getMessage());
        }

    }

    public function type(Type $type)
    {
        $project_id = 2;
        $position = $this->request->get('position', 1);
        $result = $type->field('id,title,form')->whereIn('position', [0, $position])->select()->each(function ($item) use ($position, $project_id) {
            $disease = Disease::where([
                ['type_id', '=', $item['id']],
                ['project_id', '=', $project_id],
                ['position', '=', $position]
            ])->withoutField('delete_time,create_time')->find();
            $item['result'] = null;
            if ($disease) $item['result'] = $disease;
        });
        ajaxSuccess($result);
    }

    public function typeAdd()
    {
        $title = $this->request->post('title');
        if (!$title) ajaxError('请填写标题');
        if (mb_strlen($title) > 10) ajaxError('标题长度不能超过10个字符');
        $position = $this->request->post('position', 1);
        $form = 'file';
        if ($title == '外观检测') $form = 'table1';
        else if ($title == '钢筋保护层厚度') $form = 'table2';
        Type::create([
            'title' => $title,
            'position' => $position,
            'form' => $form,
            'create_time' => time(),
        ]);
        ajaxSuccess();
    }

    protected $type = ['外观检测', '钢筋保护层厚度'];

    public function jiance(Disease $disease)
    {
        $list = $disease->where('project_id', 3)->field('id,file, title')->order('id', 'desc')->select();
        ajaxSuccess($list);
    }

    public function jianceSave(Disease $disease)
    {
        $postData = $this->request->post(['id', 'file' => '', 'title' => '']);
//        if (!$postData['file']) ajaxError('请上传文件');
        if (isset($postData['id']) && intval($postData['id']) > 0) {
            $info = $disease->where('id', $postData['id'])->find();
            if (!$info || $info['project_id'] != 3) ajaxError('请选择正确的项目');
            $info->file = $postData['file'];
            $info->title = $postData['title'];
            $info->save();
        } else {
            $postData['project_id'] = 3;
            $disease->save($postData);
        }
        ajaxSuccess();
    }

    public function jianceDel(Disease $disease)
    {
        $id = $this->request->post('id');
        if (!$id) ajaxError('请选择要删除的数据');
        $disease->whereIn('id', $id)->update(['delete_time' => time()]);
        ajaxSuccess();
    }

    /**
     * 外观检测
     * @param Disease $disease
     * @param Type $type
     * @param \app\index\validate\Disease $diseaseValide
     * @return void
     * @throws \think\db\exception\DbException
     */
    public function disease(Disease $disease, Type $type, \app\index\validate\Disease $diseaseValide)
    {
        $data = $this->request->post([
            'id' => '', 'type_id' => '', 'title' => '', 'num' => '', 'pattern' => '', 'desc' => '', 'image' => '', 'memo' => '', 'position' => 1, 'file' => ''
        ]);
        $data['project_id'] = 2;
        $typeInfo = $type->where('id', $data['type_id'])->find();
        if (!$typeInfo) ajaxError('类型不存在');
        //if (!in_array($typeInfo['title'], $this->type)) ajaxError('提交类型应选择文件');
        /*if ($typeInfo['form'] == 'table2') {
            if (!$diseaseValide->check($data, $diseaseValide->rule2)) ajaxError($diseaseValide->getError());
        } else if ($typeInfo['form'] == 'table1') {
            if (!$diseaseValide->check($data)) ajaxError($diseaseValide->getError());
        } else {
            if (!$diseaseValide->check($data, $diseaseValide->rule3)) ajaxError($diseaseValide->getError());
        }*/
        $find = $disease->where([
            ['type_id', '=', $data['type_id']],
            ['project_id', '=', $data['project_id']],
            ['position', '=', $data['position']]
        ])->find();
        if ($find && $find['id'] != $data['id']) ajaxError('此选项已添加');

        if (isset($data['id']) && $data['id'] > 0) {
            $disease->update($data, ['id' => $data['id']]);
        } else {
            $disease->save($data);
        }
        ajaxSuccess();
    }

    public function delete()
    {
        $id = $this->request->post('id');
        if (!$id) ajaxError('参数不足');
        //验证是否存在
        if ($id <= 4) ajaxError('系统类型不能删除');
        $find = Type::where('id', $id)->find();
        if (!$find) ajaxError('已删除或不存在');
        $find->delete_time = time();
        $find->save();
        //删除关联数据
        Disease::where('type_id', $id)->where('position', $find['position'])->update(['delete_time' => time()]);
        ajaxSuccess();
    }

}

